define(["t5/core/dom", "t5/core/events","jquery","core"], function(dom, events,$,core) {
    /****************************************************************
    *
    *  全部初始化调用方法，在页面加载完成后会调用此方法。
    ******************************************************************/
  	_initGlobal=function(){
  	   //页面大小修改函数
  		try{
  		  	$(window).resize(fixContentWidth);
			fixContentWidth();
  		}catch(e){}
  		//在页面加载完后最晚执行的函数
  		try{
  		   init($,core);
  		}catch(e){}
  	};
  	_combo=function(spec){
  		 $('#'+spec.id).combo(spec.params);
         $('#'+spec.id+"_div").appendTo($('#'+spec.id).combo('panel'));
         $('#'+spec.id+"_div"+' input').click(function(){
                var v = $(this).val();
                var s = $(this).next('span').text();
                $('#'+spec.id).combo('setValue', v).combo('setText', s).combo('hidePanel');
         });
         if(spec.value){
             //设置默认值
             $.each($('#'+spec.id+"_div"+' input'), function(i, n){
                  if(spec.value==n.value){
                       n.click();
                  }
			 });
         }
        // $('#'+params.id).combo('setValue', '04');
  	};
  	_combobox=function(spec){
  	     if(spec.formatter){//如果有formatter函数
  	       var formatterFun=  new Function('return '+spec.formatter)();
  	       spec.params['formatter']=formatterFun;
  	     	//$('#'+spec.id).combobox({formatter:formatterFun});
  	     }
  	     if(spec.loadFilter){
  	     	var loadFilterFun=  new Function('return '+spec.loadFilter)();
  	     	 spec.params['loadFilter']=loadFilterFun;
  	     	//$('#'+spec.id).combobox({loadFilter:loadFilterFun});
  	     }
  	     //select事件处理
  	     var loadSubList=function(newValue,ischange){
  	        $('input[parent='+spec.id+']').each(function(){
	  	         var id=$(this).attr("id");
	  	         var url=$(this).attr("url");
	  	         var urlTrue = url+ (url.indexOf("?")>0?'&':'?')+"parent="+newValue;
	  	         if(ischange){
	  	        	$('#'+id).combobox('setValue','');
	  	         }
            	 $('#'+id).combobox('reload', urlTrue);
	  	     });
  	     }

  	     var changeHandle=function(newValue,oldValue){
  	        if(spec.changeHandle){
	  	     	var changeHandleFun=  new Function('return '+spec.changeHandle)();
	  	     	changeHandleFun(newValue,oldValue);
	  	     }
	  	     loadSubList(newValue,true);
  	     }
  	     spec.params['onChange']=changeHandle;

  	    var loadsuccessHandle=function(rec){
  	        loadSubList($('#'+spec.id).combobox('getValue'),false);//跟据父初始值来初始化子列表的值
  	    }
  	    spec.params['onLoadSuccess']=loadsuccessHandle;
  	    $('#'+spec.id).combobox(spec.params);
  	};
  	_datagrid=function(spec){
	  	 //$('#'+spec.id).datagrid(spec.params);
	  	 if(spec.columns){//组装columns元素
	  	 	$.each(spec.columns,function(aryIndex,ary){
	  	 	      $.each(ary,function(colIndex,col){
	  	 	      	   //formatter的处理
	  	 	      		if('op'==col.field && spec.opFormatter){
	  	 	      			col.formatter=  new Function('return '+spec.opFormatter)();
	  	 	      		}else if(col.formatter){
	  	 	      			col.formatter=  new Function('return '+col.formatter)();
	  	 	      		}
	  	 	      	   //styler的处理
	  	 	      	    if(col.styler){
	  	 	      			col.styler=  new Function('return '+col.styler)();
	  	 	      		}
	  	 	      	   //sorter的处理
	  	 	      	    if(col.sorter){
	  	 	      			col.sorter=  new Function('return '+col.sorter)();
	  	 	      		}
	  	 	      });
	  	 	});
	  	 	spec.params['columns']=spec.columns;
	  	 	//spec.params['onBeforeLoad']=function (param){
	  	 	//	if(!param['_findAllNum']){
	  	 	//		param['_findAllNum']=-1;
	  	 	//	}
	  	 	//}
	  	   //$('#'+spec.id).datagrid({"columns":spec.columns});
	  	 }
	  	 //
	  	 if(spec.loadSuccessHandle){
	  		 var loadSuccessHandleFun=  new Function('return '+spec.loadSuccessHandle)();
	  		 spec.params['onLoadSuccess']=function(data){
	  			loadSuccessHandleFun(data);
	  			$('#'+spec.id).datagrid('options').queryParams['_findAllNum']=data.total; //设置allNum值 ,这样就不用再次计算count
	  		 };
	  	 }
	  	 if(spec.beforeLoadHandle){
	  		 var beforeLoadHandleFun=  new Function('return '+spec.beforeLoadHandle)();
	  		 spec.params['onBeforeLoad']=function(param){
	  			if(!beforeLoadHandleFun(param)){
	  				return false;
	  			}else{
	  				if(!param['_findAllNum']){//如果没有调置allNum值,就设置一次,要后强查一次count
		  				param['_findAllNum']=-1;
		  			}
	  				return true;
	  			}
	  		 };
	  	 }
	  	 $('#'+spec.id).datagrid(spec.params);
  	};
  	_combogrid=function(spec){
  		if(spec.columns){//组装columns元素
	  	 	$.each(spec.columns,function(aryIndex,ary){
	  	 	      $.each(ary,function(colIndex,col){
	  	 	      	   //formatter的处理
	  	 	      		if('op'==col.field && spec.opFormatter){
	  	 	      			col.formatter=  new Function('return '+spec.opFormatter)();
	  	 	      		}else if(col.formatter){
	  	 	      			col.formatter=  new Function('return '+col.formatter)();
	  	 	      		}
	  	 	      	   //styler的处理
	  	 	      	    if(col.styler){
	  	 	      			col.styler=  new Function('return '+col.styler)();
	  	 	      		}
	  	 	      	   //sorter的处理
	  	 	      	    if(col.sorter){
	  	 	      			col.sorter=  new Function('return '+col.sorter)();
	  	 	      		}
	  	 	      });
	  	 	});
	  	 	spec.params['columns']=spec.columns;
	  	   // $('#'+spec.id).combogrid({"columns":spec.columns});
	  	 }
	  	 $('#'+spec.id).combogrid(spec.params);
	  	 if(spec.value){//只能放一个值
	  	   // alert(spec.value);
	  	   var vals=spec.value.split(',');
	  	   var objs=[];
	  	   $.each(vals,function(i,n){
	  	      objs.push(n);
	  	   });
	  	    $('#'+spec.id).combogrid('setValues',objs);
	  	 }
  	};
  	_numberbox=function(spec){
  	   // alert(spec);

  		if(spec.filter){
  			var filterFun=  new Function('return '+spec.filter)();
  			spec.params['filter']=filterFun;
  	     	//$('#'+spec.id).numberbox({filter:filterFun});
  		}
  		if(spec.formatter){
  			var formatterFun=  new Function('return '+spec.formatter)();
  			spec.params['formatter']=formatterFun;
  	     	//$('#'+spec.id).numberbox({formatter:formatterFun});
  		}
  		if(spec.parser){
  			var parserFun=  new Function('return '+spec.parser)();
  			spec.params['parser']=parserFun;
  	     	//$('#'+spec.id).numberbox({parser:parserFun});
  		}
  		$('#'+spec.id).numberbox(spec.params);
  	};
  	_numberspinner=function(spec){
  		if(spec.spin){
  			var spinFun=  new Function('return '+spec.spin)();
  			spec.params['spin']=spinFun;
  	     	//$('#'+spec.id).numberspinner({spin:spinFun});
  		}
  		$('#'+spec.id).numberspinner(spec.params);
  	};
  	_timespinner=function(spec){
  		if(spec.spin){
  			var spinFun=  new Function('return '+spec.spin)();
  			spec.params['spin']=spinFun;
  	     	//$('#'+spec.id).numberspinner({spin:spinFun});
  		}
  		$('#'+spec.id).timespinner(spec.params);
  	};
  	_dialog=function(spec){
  		$('#'+spec.id).dialog(spec.params);
  	};
  	_queryLayout=function(spec){
  		var queryHanderTrue=function(){
  		     var checkRes=true;
	  	     if(spec.queryHandle){
	  	     	var queryHandleFun=  new Function('return '+spec.queryHandle)();
	  	     	checkRes=queryHandleFun();
	  	     }
	  	     if(checkRes){
	  	        var params = $('#'+spec.id+'_form').serializeObject();
	  	        $('#'+spec.id+'_grid').datagrid('load', params);
	  	     }
  		}
  		$('#'+spec.id+'_query').bind('click',queryHanderTrue);

  	    $('#'+spec.id+'_add').bind('click',function(){
  	    	$('#'+spec.id+'_dialog').dialog({title:msg.add});
  	    	$('#'+spec.id+'_updateform').form('clear');
  	    	$('#'+spec.id+'_dialog').dialog().css({"visibility":"visible"});//为了在显示页面是不让
  	    	//$('#'+spec.id+'_dialog').dialog().css({"display":"block"});
  	    	if(spec.initAddHandle){//新增时的初始化
  	    		var initAddHandle = new Function('return '+spec.initAddHandle)();
  	    		initAddHandle();
  	    	}
			$('#'+spec.id+'_dialog').dialog('open');
  	    });
  	};
  	_showInfo=function(spec){
  	   $.vxi.optshow(spec);
  	};
  	_uploadBind=function(spec){
  	   //文件上传控件绑定 添加 文件按钮方法
  	   index=0;
  	   $('#'+spec.id+'_addimg').click(function(){
  	   	  $('#'+spec.id+'_div').append('<span style="display:block"><img  src="'+spec.delImg+'" title="'+window.msg.deleteattachment+'" onclick="jQuery.vxi.discardElement(this.parentNode)"/><input type="file" filetag="'+spec.prefix+'"  name="'+spec.prefix+(index++)+'"/></span>');
  	   });
  	   //设置form的属性
  	   if(!document.getElementById(spec.id).form){
  	      $.vxi.alert(window.msg.uploadneedtitle);
  	   }else{
	  	  var formId=document.getElementById(spec.id).form.id;
	  	   if(formId){
	  	      $('#'+formId).attr("enctype","multipart/form-data");
	  	   }
  	   }
  	};
  	_tree=function(spec){
  		$.each(spec.params,function(name, value){//把字符串转为事件
	  	    if(name.startWith("on")){
	  	    	var tempFun=  new Function('return '+value)();
	  	    	spec.params[name]=tempFun;
	  	    }
	    });
  		if(spec.formatter){
  			var formatterFun=  new Function('return '+spec.formatter)();
  			spec.params['formatter']=formatterFun;
  		}
  		$('#'+spec.id).tree(spec.params);
  		//alert(spec);
  	};
  	return {initGlobal:_initGlobal,combobox:_combobox,combo:_combo,datagrid:_datagrid,numberbox:_numberbox,numberspinner:_numberspinner,timespinner:_timespinner,dialog:_dialog,combogrid:_combogrid,queryLayout:_queryLayout,showInfo:_showInfo,uploadBind:_uploadBind,tree:_tree};
  });